Preserve channels-last memory format in to_image - #9637
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/vision/9637
Note: Links to docs will display an error until the docs builds have been completed. This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
Hi @Hemanath400! Thank you for your pull request and welcome to our community. Action RequiredIn order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you. ProcessIn order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
|
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks! |
32d7591 to
d9b44cf
Compare
fallenmi
left a comment
There was a problem hiding this comment.
Reviewed exact head d9b44cf1e1f406df2b1f5798926afba4c3e54c51 against exact base ac8d215f7d45d6601451b62e9f81622dac8aa0b4.
Removing the forced contiguous copy preserves the ordinary NumPy HWC storage as a CHW-shaped tensor with channels-last strides. In an independent matrix, standard RGB, RGBA, and float32 HWC arrays become channels-last after adding the batch dimension on this head, while base converts them to contiguous channels-first storage.
I also exercised eight NumPy layouts—including grayscale, row-padded ROIs, strided channels/columns, Fortran order, and float32—through to_image, horizontal flip, resize, dtype conversion, and normalization. Base, head, and the current GitHub merge produced identical shapes and value digests for every applicable operation; the head preserves each input's actual strides rather than changing values. The focused TestToImage slice passes 11/11 on head and merge (10/10 on base), git diff --check is clean, and the current merge tree is byte-identical to the reviewed head.
GitHub currently exposes only the green CLA/import/internal-only checks for this PR, not the normal public test matrix; the exact local functional coverage above is therefore the basis of this approval. I found no blocking issue.
AI disclosure: I used OpenAI Codex to inspect the exact revisions and repository policy, construct and run the stride/layout and downstream-transform matrix, execute the focused tests, and draft this review. I verified the results and conclusion.
Description
This PR avoids forcing a contiguous copy when converting a NumPy image to a torchvision
Imageinto_image.Currently, the NumPy input path uses
.contiguous()afterpermute(), which converts the tensor to a channels-first contiguous memory layout.Removing
.contiguous()preserves the original HWC memory layout while keeping the returned tensor in CHW shape, allowing the resulting tensor to be channels-last compatible.Changes
.contiguous()call from the NumPy input path into_image.Testing
TestToImage: 11 passedgit diff --check: passedThe full
test_transforms_v2.pysuite was also run. The unrelated failures were due to the local build not having libjpeg support enabled.Fixes #9547